package com.web.admin.web.base.auth;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.http.message.ViewBuilder;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.facade.MenuButtonFacade;
import com.web.base.auth.facade.MenuFacade;
import com.web.base.auth.message.MenuButtonRequest;
import com.web.base.auth.message.MenuButtonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("authMenuButtonController")
@Http(url = "/base/auth")
public class MenuButtonController
{
	@Autowired
	private MenuButtonFacade menuButtonFacade;
	@Autowired
	private MenuFacade menuFacade;

	private DictParser dictParser = DictParser.getInstance();

	public MenuButtonController()
	{
	}
	
	@Http(url = "/menu_button_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		MenuButtonRequest menuButtonRequest = request.getQueryObject(MenuButtonRequest.class);
		ViewBuilder builder = View.createBuilder();
		if (menuButtonRequest.getMenuId() != null)
		{
			builder.put("parentModel", menuFacade.get(menuButtonRequest.getMenuId()));
		}
		return builder.setView("/base/auth/menu_button_list").put("model", menuButtonRequest)
					.put("map", dictParser.getStringMap())
					.put("modelList", menuButtonFacade.list(menuButtonRequest))
					.build();
	}
	
	@Http(url = "/menu_button_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		MenuButtonRequest menuButtonRequest = request.getQueryObject(MenuButtonRequest.class);
		MenuButtonResponse response = null;
		if (menuButtonRequest.getId() != null)
		{
			response = menuButtonFacade.get(menuButtonRequest.getId());
		}
		else
		{
			response = new MenuButtonResponse();
			response.setMenuId(menuButtonRequest.getMenuId());
		}
		ViewBuilder builder = View.createBuilder();
		if (response.getMenuId() != null)
		{
			builder.put("parentModel", menuFacade.get(response.getMenuId()));
		}
		return builder.setView("/base/auth/menu_button_get").put("model", response)
					.put("map", dictParser.getStringMap())
					.build();
	}
	
	@Http(url = "/menu_button_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		MenuButtonRequest menuButtonRequest = request.getPostObject(MenuButtonRequest.class);
		MenuButtonResponse response = null;
		if (menuButtonRequest.getId() == null)
		{
			ValidationUtil.valid(menuButtonRequest, SaveModel.class);
			response = menuButtonFacade.add(menuButtonRequest);
		}
		else
		{
			ValidationUtil.valid(menuButtonRequest, UpdateModel.class);
			response = menuButtonFacade.update(menuButtonRequest);
		}
	}
	
	@Http(url = "/menu_button_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		int id = request.getPostInt("id", 0);
		menuButtonFacade.delete(id);
	}
	
}
